package com.baosight.sczx.zm.cp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cp.domain.ZMCPAttachmanag;

public class ServiceZMCP06 extends ServiceEPBase {

	/**
	 * 初始化
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo  = this.query(inInfo);
		// 编制人 -- 登录人
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.addBlock(StaticBlocks.getPlanMonth("cmes.cp.planMonth", true));
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.planStatus", new String[]{"草拟","生效","作废"},new String[]{"草拟","生效","作废"},true));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.plantype", true));
		return outInfo;
	}
	/**
	 * 查询
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdProduceplanId =null;
		//添加进查询条件 
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		StaticTools.setQueryParameters(inInfo, "fdMiningName", UserSession.getInSessionProperty("miningName"), true);
		
		//添加fdProduceplanId进查询条件
		fdProduceplanId = inInfo.getString("fdProduceplanId");
		String fdPlanYear = inInfo.getString("fdPlanYear");
		String fdPlanMonth = inInfo.getString("fdPlanMonth");
		StaticTools.setQueryParameters(inInfo, "fdProduceplanId", fdProduceplanId, true);
		StaticTools.setQueryParameters(inInfo, "fdPlanYear", fdPlanYear, true);
		StaticTools.setQueryParameters(inInfo, "fdPlanMonth", fdPlanMonth, true);
		EiInfo outInfo = new EiInfo();
		outInfo=super.query(inInfo, "ZMCPAttachmanag.query", new ZMCPAttachmanag());
		//计划类型
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.plantype", true));
		outInfo.addBlock(StaticBlocks.getPlanMonth("cmes.cp.planMonth", true));
		//编制部门
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,
				UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));

		return outInfo;
	}
	
	/**
	 * 插入
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//获取result域的值
		EiInfo out = new EiInfo();
		ZMCPAttachmanag attach = new ZMCPAttachmanag();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(attach.eiMetadata);
		int rows = eiBlock.getRowCount();
		Map<String, String> map = new HashMap<String, String>();
		for (int i=0; i < rows; i++) {
			attach.fromMap(eiBlock.getRow(i));
			map.put("fdShow", attach.getFdShow().toString());
			map.put("fdPlanYear", attach.getFdPlanYear());
			map.put("fdPlanMonth", attach.getFdPlanMonth());
			map.put("fdPlanType", attach.getFdPlanType());
			map.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString());
			//判断是否存在
			boolean validate = this.validate(attach, "ZMCPAttachmanag.query", map, "query");
			if(!validate){
				attach.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
				attach.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
				attach.setFdCreatorId(UserSession.getUserId().toString());
				attach.setFdPulisher(UserSession.getInSessionProperty("UserCname").toString());
				attach.setFdCreateDepartment(Department.getInstance().getDepartmentNameById(attach.getFdCreateDepartmentId()));
				attach.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_ATTACHMANAG"));
				attach.setFdDocid(StaticTools.getDocId());
				attach.setFdProduceplanId(inInfo.getString("fdProduceplanId"));
				attach.setFdPulisherId( (String)UserSession.getUserId());
				dao.insert("ZMCPAttachmanag.insert", attach);
			}
			else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg(attach.getFdTitle()+"已经存在！");
				return out;
			}
			}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rows + "记录成功");
		return out;
	}
	
	/**
	 * 更新
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCPAttachmanag attach = new ZMCPAttachmanag();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(attach.eiMetadata);
		int rows = eiBlock.getRowCount();
		Map<String,String> map = new HashMap<String, String>();
		for(int i=0;i<rows;i++){
			attach.fromMap(eiBlock.getRow(i));
			map.put("fdKeyId", attach.getFdKeyId());
			map.put("fdShow", attach.getFdShow().toString());
			map.put("fdPlanYear", attach.getFdPlanYear());
			map.put("fdPlanMonth", attach.getFdPlanMonth());
			map.put("fdPlanType", attach.getFdPlanType());
			map.put("fdMiningId", UserSession.getInSessionProperty("miningCode").toString());
			boolean validate = this.validate(attach, "ZMCPAttachmanag.updateValidate", map, "update");
			if(!validate){
				attach.setFdCreatorId(UserSession.getUserId().toString());
				attach.setFdPulisher(UserSession.getInSessionProperty("UserCname").toString());
				attach.setFdCreator(UserSession.getInSessionProperty("UserCname").toString());
				attach.setFdPulisherId( (String)UserSession.getUserId());
				attach.setFdCreateDepartment(Department.getInstance().getDepartmentNameById(attach.getFdCreateDepartmentId()));
				dao.update("ZMCPAttachmanag.update", attach);
				}
			else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg(attach.getFdTitle()+"已经存在！");
				return out;
			}
			}
		out=this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rows + "记录成功");
		return out;
	}
	
	/**
	 * 删除
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		out = super.delete(inInfo, "ZMCPAttachmanag.delete");
		out.setStatus(1);
		out=this.query(inInfo);
		out.setMsg("删除操作成功");
		return out;
	}
	//退回
	public EiInfo revert(EiInfo inInfo){
		EiInfo out = new EiInfo();
		ZMCPAttachmanag attach = new ZMCPAttachmanag();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(attach.eiMetadata);
		int rows = eiBlock.getRowCount();
		Map<String,String> map = new HashMap<String, String>();
		for(int i=0;i<rows;i++){
			attach.fromMap(eiBlock.getRow(i));
			map.put("fdPlanYear", attach.getFdPlanYear());
			map.put("fdPlanMonth", attach.getFdPlanMonth());
			map.put("fdPlanType", attach.getFdPlanType());
			map.put("fdCreatorId", attach.getFdCreatorId());
			//判断退回操作是否是上报人，是则不能退回
			if(attach.getFdPulisherId()!= null && attach.getFdPulisherId().equals(UserSession.getUserId().toString())) {
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("不能退回自己所上报数据");
			}
			else{
				if (attach.getFdShow().toString().equals("1")) {
					// 更新05数据状态
					this.dao.update("ZMCPAttachmanag.updatePlan", map);
					map.put("fdProduceplanId", attach.getFdProduceplanId());
					// 删除计划数据
					this.dao.delete("ZMCPAttachmanag.revert", map);
					out = this.query(inInfo);
					out.setStatus(1);
					out.setMsg("操作" + rows + "记录成功");
				} else {
					out = this.query(inInfo);
					out.setStatus(1);
					out.setMsg("只能对上报的数据进行退回");
			}
		}
	}
		return out;  
	}
	/**
	 * Description: 对主表记录新增，删除，上报前的后台校验
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(ZMCPAttachmanag _dom, String sqlName, Map<String, String> params, String type) {
		if (params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query(sqlName, params);
		if (result != null && result.size() > 0 && !"report".equals(type)) {
			return true;
		}
		return false;
	}
}
